---
title: "Balancing algorithm report"
output:
  html_document:
    df_print: paged
editor_options:
  chunk_output_type: console
---

```{r,message=FALSE,echo=FALSE}
library(dplyr)
```

```{r,echo=FALSE,message=FALSE,warning=FALSE}
df <- read.csv("croatia_clean.csv")

est <- read.csv("Croatiaestimates.csv")
est$est=est$est/100
est$Freq=nrow(df) * est$est
est$est=NULL
X <- split(est, est$variable)
X=lapply(X, function(x) { x["variable"] <- NULL; x })
list2env(X ,.GlobalEnv)
joint.d=rename(joint, joint=label)
pol.d=rename(pol, pol=label)
sex.d=rename(sex, sex=label)
vot.d=rename(vot, vot=label)

library(survey)
df$ids=seq(1:nrow(df))
df1 <- svydesign(ids=~1, data=df)

df.r <- rake(design = df1,
             sample.margins = list(~joint, ~pol, ~sex, ~vot),
             population.margins = list(joint.d, pol.d, sex.d, vot.d))

df.r1 <- trimWeights(df.r, lower=0.6, upper=5,
                                   strict=TRUE) 
df$w=weights(df.r1)
df$Freq=round(df$w,0)

df0=select(df,Record.ID,joint,pol,sex,vot,Freq)
MyData <- df0[rep(row.names(df0), df0$Freq), 1:5]
write.csv(MyData,"MyData.csv",row.names=F)

```

# Python (config = code) 
```{python engine.path="/usr/bin/python3.5",results='hide'}
import sys
sys.path.append('/home/shared/data/balancing/')
import drop
config = "{1: {'degreeHigh':7.4,'noDegreeHigh':41.8,'degreeLow':10.2,'noDegreeLow':40.5}},{2: {'interest':35.7,'LowInterest':64.3}},{3: {'Male':48.7,'Female':51.3}},{4: {'HDZ':35.46,'KUK':25.62,'ORaH':8.06,'Savez':5.89,'LAB':2.92,'NF':2.05,'Undec':20.00}}"

filename = '/home/fernando/Misc/bjpolFinal/Croatia/MyData.csv'

drop.run(config, filename, "/home/fernando/Misc/bjpolFinal/Croatia/myBal", 2.5)
```

